package com.smartleanx.module.hrm.controller.admin.examine.examineAssessmentScope;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineAssessmentScope.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineAssessmentScope.ExamineAssessmentScopeDO;
import com.smartleanx.module.hrm.service.examine.examineAssessmentScope.ExamineAssessmentScopeService;

@Tag(name = "管理后台 - 考核范围")
@RestController
@RequestMapping("/hrm/examine-assessment-scope")
@Validated
public class ExamineAssessmentScopeController {

    @Resource
    private ExamineAssessmentScopeService examineAssessmentScopeService;

    @PostMapping("/create")
    @Operation(summary = "创建考核范围")
    @PreAuthorize("@ss.hasPermission('hrm:examine-assessment-scope:create')")
    public CommonResult<Integer> createExamineAssessmentScope(@Valid @RequestBody ExamineAssessmentScopeSaveReqVO createReqVO) {
        return success(examineAssessmentScopeService.createExamineAssessmentScope(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核范围")
    @PreAuthorize("@ss.hasPermission('hrm:examine-assessment-scope:update')")
    public CommonResult<Boolean> updateExamineAssessmentScope(@Valid @RequestBody ExamineAssessmentScopeSaveReqVO updateReqVO) {
        examineAssessmentScopeService.updateExamineAssessmentScope(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核范围")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-assessment-scope:delete')")
    public CommonResult<Boolean> deleteExamineAssessmentScope(@RequestParam("id") Integer id) {
        examineAssessmentScopeService.deleteExamineAssessmentScope(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核范围")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-assessment-scope:query')")
    public CommonResult<ExamineAssessmentScopeRespVO> getExamineAssessmentScope(@RequestParam("id") Integer id) {
        ExamineAssessmentScopeDO examineAssessmentScope = examineAssessmentScopeService.getExamineAssessmentScope(id);
        return success(BeanUtils.toBean(examineAssessmentScope, ExamineAssessmentScopeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核范围分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-assessment-scope:query')")
    public CommonResult<PageResult<ExamineAssessmentScopeRespVO>> getExamineAssessmentScopePage(@Valid ExamineAssessmentScopePageReqVO pageReqVO) {
        PageResult<ExamineAssessmentScopeDO> pageResult = examineAssessmentScopeService.getExamineAssessmentScopePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineAssessmentScopeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核范围 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-assessment-scope:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineAssessmentScopeExcel(@Valid ExamineAssessmentScopePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineAssessmentScopeDO> list = examineAssessmentScopeService.getExamineAssessmentScopePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核范围.xlsx", "数据", ExamineAssessmentScopeRespVO.class,
                        BeanUtils.toBean(list, ExamineAssessmentScopeRespVO.class));
    }

}